<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" size="small" ref="queryForm" label-width="88px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="选择矩阵" prop="classify">
                            <el-select v-model="queryParams.classify" :placeholder="$t('public.selectPl')+$t('mtool.standard')" @change="handleQuery">
                                <el-option v-for="dict in dict.type.NYIQC_MATRIX_TYPE" :key="dict.value" :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="厂区" prop="factoryName">
                            <el-select v-model="queryParams.factoryName" placeholder="请选择厂区" filterable @change="changeFactory">
                                <el-option v-for="dict in factoryOptions" :key="dict.factoryName" :label="dict.factoryName" :value="dict.factoryName" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!--            用户组-->
                        <el-form-item label="用户组" prop="groupId">
                            <el-select v-model="queryParams.groupId" placeholder="请选择用户组" @change="handleQuery" filterable>
                                <el-option v-for="dict in userGroupOptions" :key="dict.id" :label="dict.groupName" :value="dict.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!--            物料大类-->
                        <el-form-item label="物料大类" prop="materialMax">
                            <el-select v-model="queryParams.materialMax" placeholder="请选择物料大类" @change="handleQuery" filterable>
                                <el-option v-for="(item,index) in dict.type.NYIQC_MATERIAL_MAX" :key="index" :label="item.label" :value="item.label" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="6">
                        <el-form-item class="right-btns">
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.query')}}</el-button>
                            <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
                        </el-form-item>
                    </el-col> -->
                </el-row>
            </el-form>
        </div>
        <div class="app-container">
            <div class="container-header">
                <div class="header-left">
                    检验员技能矩阵
                </div>
                <div class="last-update">
                    最后更新时间：{{editTime}}
                </div>
            </div>
            <el-row :gutter="10" class="table-action-wrap btnGroup">
                <el-col :span="4">
                    <el-button class="export-op exportitemBtn" type="default" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['ny-masterdata:gaugeMatrix:export']"><img src="@/assets/images/icons/daochu.png" alt=""> {{$t('public.export')}}</el-button>
                </el-col>
                <el-col :span="20" align="right">
                    <el-popover width="400" placement="bottom" trigger="hover">
                        <div class="icon-tip" slot="reference">
                            <img v-for="item in tipsList" :src="require(`@/assets/images/skill/reality${item.reality}.png`)" alt="">
                        </div>
                        <div>
                            <div class="tip-head"><span>计划</span><span>实际</span></div>
                            <el-divider></el-divider>
                            <el-table max-height="350" :data="tipsList" :border="false" :show-header="false" class="tip-table">
                                <el-table-column label="计划" prop="plan" width="50" align="center">
                                    <template slot-scope="scope">
                                        <img :src="require(`@/assets/images/skill/plan${scope.row.plan}.png`)" alt="">
                                    </template>
                                </el-table-column>
                                <el-table-column label="实际" prop="reality" width="50" align="center">
                                    <template slot-scope="scope">
                                        <img :src="require(`@/assets/images/skill/reality${scope.row.reality}.png`)" alt="">
                                    </template>
                                </el-table-column>
                                <el-table-column prop="desc" />
                            </el-table>

                        </div>
                    </el-popover>
                </el-col>
            </el-row>
            <div class="container-table">

                <el-table max-height="350" border stripe v-loading="loading" :data="typeList" @selection-change="handleSelectionChange" :span-method="arraySpanMethod">

                    <!--              <el-table-column type="selection" width="55" align="center" />-->
                    <!--          序号-->
                    <el-table-column :label="$t('public.serialNum')" type="index" width="50" align="center" />
                    <!--          姓名-->
                    <el-table-column label="姓名" prop="userName" width="100" align="center" show-overflow-tooltip />
                    <!--          负责区域-->
                    <el-table-column label="负责区域" prop="territory" width="140" align="center" show-overflow-tooltip />
                    <!--          接替人-->
                    <el-table-column label="接替人" prop="replaceMan" width="100" align="center" show-overflow-tooltip />
                    <el-table-column v-for="(item,index) in columns" :key="item.key" :label="item.materialMax" :prop="item.key" minWidth="120" align="center">
                        <el-table-column v-for="jtem in item.middleList" :key="jtem.key" :label="jtem.materialMiddle" :prop="jtem.key" width="120" align="center">
                            <el-table-column v-for="ktem in jtem.minList" :key="ktem.key" :label="ktem.typeName" :prop="ktem.key" width="120" align="center">
                                <template slot-scope="scope">
                                    <skill :classify="type" :plan="scope.row[ktem.typeCode].planGauge" :reality="scope.row[ktem.typeCode].practicalGauge" :uid="scope.row.qmsUserId" :ukey="ktem.typeCode" @update="update"></skill>
                                </template>
                            </el-table-column>
                        </el-table-column>
                    </el-table-column>

                    <!--              每个人可操作岗位-->
                    <el-table-column label="每个人可操作岗位(1/2技能)" prop="name" minWidth="120" align="center">
                        <el-table-column label="计划" prop="plan" minWidth="120" align="center"></el-table-column>
                        <el-table-column v-if="type != 1" label="实际" prop="practical" minWidth="120" align="center"></el-table-column>
                    </el-table-column>
                    完成率
                    <el-table-column v-if="type != 1" label="完成率" prop="completion" minWidth="120" align="center" />
                    <el-table-column v-if="type != 1" label="达100%的比例" prop="ratio" minWidth="120" align="center">
                        <template slot-scope="scope">
                            {{ratio}}
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />

                <el-table :height="tableHeight" :data="precentList" border :span-method="precentSpanMethod" stripe :show-header="false">
                    <el-table-column prop="a1" width="195" align="center" class-name="bg-gray">
                        <template slot-scope="scope">
                            <div class="bold">{{scope.row.a1}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="a2" width="195" align="center" class-name="bg-gray">
                        <template slot-scope="scope">
                            <div class="bold">{{scope.row.a2}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column v-for="(item,index) in columns" :key="item.key" :label="item.gaugeType" :prop="item.key" minWidth="120" align="center">
                        <!-- <el-table-column v-for="jtem in item.messageList" :key="jtem.key" :label="jtem.deviceName+jtem.key" :prop="jtem.key" width="120" align="center">
                            <template slot-scope="scope">
                                <span v-if="scope.$index == 3">{{scope.row['processRatio']}}</span>
                                <span v-else>{{scope.row[jtem.key]}}</span>
                            </template>
                        </el-table-column> -->
                        <el-table-column v-for="jtem in item.middleList" :key="jtem.key" :label="jtem.materialMiddle" :prop="jtem.key" width="120" align="center">
                            <el-table-column v-for="ktem in jtem.minList" :key="ktem.key" :label="ktem.typeName" :prop="ktem.key" width="120" align="center">
                                <template slot-scope="scope">
                                    <span v-if="scope.$index == 3">{{scope.row['processRatio']}}</span>
                                    <span v-else>{{scope.row[ktem.typeCode]}}</span>
                                </template>
                            </el-table-column>
                        </el-table-column>
                    </el-table-column>
                </el-table>
            </div>

            <!-- 新增|编辑｜查看 弹框 -->
            <!--      <mtool-detail :visible.sync="visible" v-model="form" :type="dialogType" @refresh="getList()"></mtool-detail>-->

        </div>
    </list-layout>
</template>
<script>
import {refreshCache,} from '@/api/basic/dict/type'
import {getGaugeMatrixStat, listGaugeMatrix,} from '@/api/basic/gaugeMatrix'
import {treeListGaugeMessage} from '@/api/basic/gaugeMessage'
import {listAllUserGroup} from '@/api/basic/userGroup'
import {listALLFactory} from '@/api/basic/factory'
import {modInspectorGauge} from '@/api/basic/inspectorGauge'
import skill from './skill.vue'
import {mapState} from 'vuex'
//基础数据-检验员量具矩阵
export default {
    name: 'gaugeMatrix',
    dicts: ['NYIQC_MATRIX_TYPE', 'NYIQC_MATERIAL_MAX'],
    components: { skill },
    computed: {
        ...mapState({
            isFold: (state) => state.app.isFold,
        }),
    },
    watch: {
        isFold(val) {
            if (val === true) {
                this.tableHeight = 'calc(100vh - 308px)'
            } else {
                this.tableHeight = 'calc(100vh - 396px)'
            }
        },
    },
    data() {
        return {
            tipsList: [
                { plan: 0, reality: 0, desc: '培训中，不能上岗操作' },
                {
                    plan: 1,
                    reality: 1,
                    desc: '能完成主要标准作业可在U等级人员带领下上岗',
                },
                { plan: 2, reality: 2, desc: '具备独立上岗资质，办上岗证' },
                {
                    plan: 3,
                    reality: 3,
                    desc: '具备培训他人便携作业指导书的能力',
                },
            ],
            ratio: undefined,
            tableHeight: 'calc(100vh - 308px)',
            editTime: '',
            type: '3', //3:对比  2：实际 1：计划
            dialogForm: {},
            factoryOptions: [],
            userGroupOptions: [],
            columns: [],
            precentList: [],
            direction: 'rtl',
            addInspection: false,
            visible: false,
            drawer: false,
            approvalOpen: false,
            viewModel: {
                inspectionInformation: '0',
            },
            editModel: {
                inspectionInformation: '0',
            },
            approvalForm: {
                status: '1',
                textarea: '',
            },
            selBtn: 1,
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                factoryName: '',
                groupId: '',
                classify: '3', //对比
            },
            // 表单参数
            form: undefined,
            exportLoading: false,
            fullscreen: false,
        }
    },
    created() {
        console.log(
            'NYIQC_MATRIX_TYPE====>',
            this.dict.type.NYIQC_MATRIX_TYPE
        )
        this.getFoctorySelect()
    },
    mounted() {},
    methods: {
        getFoctorySelect() {
            listALLFactory().then((res) => {
                this.factoryOptions = res.data
                this.$set(
                    this.queryParams,
                    'factoryName',
                    this.factoryOptions[0].factoryName
                )
                this.getGroup(this.factoryOptions[0].factoryName)
            })
        },
        getGroup(factoryName) {
            listAllUserGroup({ factoryName }).then((res) => {
                this.userGroupOptions = res.data
                if (this.userGroupOptions.length == 0) {
                    this.queryParams.groupId = undefined
                } else {
                    this.$set(
                        this.queryParams,
                        'groupId',
                        this.userGroupOptions[0].id
                    )
                }
                this.getList()
            })
        },
        changeFactory(e) {
            this.getGroup(this.queryParams.factoryName)
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            const span = {
                rowspan: 1,
                colspan: 1,
            }
            if (column.property == 'ratio') {
                if (rowIndex == 0) {
                    span.rowspan = this.queryParams.pageSize
                } else {
                    span.rowspan = 0
                }
            }
            return span
        },
        precentSpanMethod({ row, column, rowIndex, columnIndex }) {
            const span = {
                rowspan: 1,
                colspan: 1,
            }
            if (rowIndex == 0 && columnIndex == 0) {
                span.rowspan = 2
            }
            if (rowIndex == 1 && columnIndex == 0) {
                span.rowspan = 0
            }
            if (rowIndex > 1) {
                if (columnIndex == 0) {
                    span.colspan = 2
                }
                if (columnIndex == 1) {
                    span.colspan = 0
                }
            }
            if (rowIndex > 2) {
                if (columnIndex == 2) {
                    span.colspan = 20
                    span.rowspan = 1
                } else if (columnIndex > 2) {
                    span.colspan = 0
                    span.rowspan = 0
                }
            }
            return span
        },
        drawerClose() {
            this.drawer = false
        },
        getList() {
            this.loading = false
            treeListGaugeMessage({
                materialMax: this.queryParams.materialMax,
            }).then((res) => {
                this.columns = res.data
            })
            Promise.all([
                listGaugeMatrix(this.queryParams),
                getGaugeMatrixStat(this.queryParams),
            ]).then((res) => {
                if (res[0].code == 200) {
                    this.total = res[0].total
                    this.loading = false
                }
                if (res[1].code == 200) {
                    this.precentList = [
                        {
                            a1: '单工序培训计划(1/2)技能',
                            a2: '计划',
                            ...res[1].data.processPlan,
                        },
                    ]
                    if (this.queryParams.classify != 1) {
                        this.precentList = this.precentList.concat([
                            { a2: '实际', ...res[1].data.processPractical },
                            { a1: '完成率', ...res[1].data.processCompletion },
                            {
                                a1: '比例(达100%的比例)',
                                processRatio: res[1].data.processRatio,
                            },
                        ])
                    }
                    this.editTime = res[1].data.editTime
                    this.ratio = res[1].data.ratio
                }
                if (res[0].code == 200 && res[1].code == 200) {
                    this.typeList = res[0].rows.map((i) => {
                        i.ratio = res[1].data.ratio
                        return i
                    })
                }
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                dictId: undefined,
                dictName: undefined,
                dictType: undefined,
                status: '0',
                remark: undefined,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.type = this.queryParams.classify
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },

        handleApproval() {
            if (this.ids.length === 0) {
                this.$modal.msgWarning(
                    this.$t('inspectionManagement.pleaseSelectOne')
                )
                return
            }
            this.handleDelete()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            // this.single = selection.length!=1
            // this.multiple = !selection.length
        },

        locationRemoteMethod(e) {
            listALLGaugeMatrix({ locationName: e }).then((res) => {
                this.locationOptions = res.data
            })
        },
        handleGaugeMatrixChange(e) {
            this.queryParams.locationName = e.join(',')
        },
        handleGaugeTypeChange(e) {
            this.queryParams.gaugeType = e.join(',')
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const dictIds = (row && row.id) || this.ids
            this.$modal
                .confirm('是否确认删除id为"' + dictIds + '"的数据项？')
                .then(function () {
                    return delGauge(dictIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('processDefinition.successfullyDeleted'))
                })
                .catch(() => {})
        },

        /** 关闭抽屉操作 */
        handleClose(row) {
            this.drawer = false
        },
        /** 导出按钮操作 */
        handleExport() {
            // const queryParams = this.queryParams
            // this.$modal.confirm(this.$t('GaugeMatrix.export')).then(() => {
            //   this.exportLoading = true
            //   return exportGaugeMatrix(queryParams)
            // }).then(response => {
            //   this.$download.name(response.msg)
            //   this.exportLoading = false
            // }).catch(() => {})
            this.download(
                'ny-iqc/gaugeMatrix/export',
                {
                    ...this.queryParams,
                },
                `gaugeMatrix_${new Date().getTime()}.xlsx`
            )
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
            refreshCache().then(() => {
                this.$modal.msgSuccess('刷新成功')
            })
        },
        groupRemoteMethod(val, cb) {
            const { factoryName } = this.queryParams
            listAllUserGroup({ groupName: val, factoryName: factoryName }).then(
                (res) => {
                    this.userGroupOptions = res.data.map((i) => i.groupName)
                    cb(res.data)
                }
            )
        },
        handleGroupSelect(e) {
            this.queryParams.groupName = e.groupName
        },
        handleGroupBlur(e) {
            const { userGroupOptions, queryParams } = this
            if (!userGroupOptions.includes(queryParams.groupName)) {
                this.queryParams.groupName = ''
            }
        },

        //厂区模糊查询
        factoryRemoteMethod(val, cb) {
            listALLFactory({ factoryName: val }).then((res) => {
                this.factoryOptions = res.data.map((i) => i.factoryName)
                cb(res.data)
            })
        },

        handleFactorySelect(e) {
            this.queryParams.factoryName = e.factoryName
        },
        handleFactoryBlur(e) {
            // const { factoryOptions, queryParams } = this
            // if(!factoryOptions.includes(queryParams.factoryName)){
            //   this.queryParams.factoryName = ""
            // }
        },
        update(params, cb) {
            modInspectorGauge({
                groupId: this.queryParams.groupId,
                ...params,
            }).then((res) => {
                cb()
                if (res.code == 200) {
                    this.getList()
                }
            })
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    //float: left;
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    padding: 3px 0 10px;
    display: inline-flex;
    justify-content: center;
    border-bottom: 2px solid #0b7be3;
}
.header-right {
    float: right;
}
.container-header {
    //float: left;
    //width: 120%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #ededf2;
    //
}
.container-table {
    //float: left;
    //margin-top:6px;
    width: 100%;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 120%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 120%;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}

.right-btns ::v-deep .el-button {
    margin-top: 0;
}
.app-container {
    padding: 0;
}
.last-update {
    font-size: 13px;
    color: #999;
    line-height: 42px;
}
.icon-tip {
    display: inline-flex;
    align-items: center;
    padding: 5px;
    border: 1px solid #e0e0e0;
}
.tip-table img {
    width: 20px;
    height: 20px;
    vertical-align: -6px;
}
.tip-head {
    font-size: 13px;
    margin-bottom: 10px;
}
.tip-head span {
    display: inline-block;
    width: 50px;
    text-align: center;
}
.icon-tip img {
    width: 20px;
    height: 20px;
    margin-right: 7px;
}
.icon-tip img:last-child {
    margin: 0;
}
.export-op {
    margin-top: 2px;
}
.ops-wrap {
    margin: 5px 0;
}
::v-deep .el-divider--horizontal {
    margin: 0 0 5px;
}
.tip-table ::v-deep .el-table__cell {
    border: 0;
}
.tip-table.el-table {
    border: 0;
}
.bold {
    font-weight: bold;
}
.bg-gray {
    background-color: gray;
}
.table-action-wrap {
    padding: 12px 0;
}
</style>
